#include "testWindow.h"

QSize TestGLWidget::minimumSizeHint() const
{
    return QSize(5, 5);
}

QSize TestGLWidget::sizeHint() const
{
    return QSize(400, 400);
}

void TestGLWidget::initializeGL()
{
   glewInit();
    glClearColor(0.f,0.f,1.f,1.f);

    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    glShadeModel(GL_SMOOTH);

    initScene();
}

void TestGLWidget::initScene()
{

}

void TestGLWidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT );

    glBegin(GL_TRIANGLES);

    glVertex3f(-0.5f, 0.0f, 0.0f); // lower left vertex
    glColor3f(1.0,0.0,0.0);
    glVertex3f( 0.0f, 0.5f, 0.0f); // lower right vertex
    glColor3f(0.0,1.0,0.0);
    glVertex3f( 0.5f, 0.0f, 0.0f); // upper vertex
    glColor3f(0.0,0.0,1.0);
    glEnd();

}

void TestGLWidget::resizeGL(int width, int height)
{

    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    //glOrtho(-0.5, +0.5, -0.5, +0.5, 5, -5);
    glOrtho(-1, 1, -1, 1, 1, -1);

    glMatrixMode(GL_MODELVIEW);
}



/*-----------------------------------TestWindow-------------------------------------*/
TestWindow::TestWindow()
{
    setWindowTitle(tr("TestWindow"));
}
TestWindow::~TestWindow()
{
    std::vector<TestGLWidget*>::iterator iter;
    for(iter = m_glWidgets.begin(); iter != m_glWidgets.end();)
    {
        if(*iter)
        {
            delete *iter;
            iter = m_glWidgets.erase(iter);
            std::cout<<"Delete a case, remain test count: "<<m_glWidgets.size()<<std::endl;
        }
        else
        {
            ++iter;
        }
    }
}

void TestWindow::add(TestGLWidget* testWidget)
{
    if(testWidget)
    {
        m_glWidgets.push_back(testWidget);
        std::cout<<"case count: "<<m_glWidgets.size()<<std::endl;
    }
}
void TestWindow::setWidget()
{
    if(m_curIter != m_glWidgets.end())
    {
        QLayout *l = layout() ;
        if(l) delete l;
        l = new QHBoxLayout;
        l->addWidget((QWidget*)(*m_curIter));
        setLayout(l);
        QTimer::singleShot(1000, this, SLOT(nextWidget()));
    }

}

void TestWindow:: run()
{
    m_curIter = m_glWidgets.begin();
    setWidget();
    show();
}

void TestWindow::nextWidget()
{
    std::cout<<"Swith to the next case"<<std::endl;
    ++m_curIter;
    setWidget();
}
